package com.example.chatweb.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.IdClass;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Data
@IdClass(PersonalBaseKey.class)
@TableName(value = "personalbase")
@AllArgsConstructor
@NoArgsConstructor
public class PersonalBase {
    @TableField(value = "pdf_uuid")
    private Long pdfUuid;
    @TableField(value = "hub_uuid")
    private Long hubUuid;
    @JsonIgnore
    private String tags;
    private String doi;
    private String title;
    @JsonIgnore
    private String keyword;
    @JsonIgnore
    private String author;
    @JsonIgnore
    private String affiliation;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "UTC")
    @TableField(value = "public_time")
    private Date publicTime; // 后端存储的Date对象
    @JsonIgnore
    private transient String publicTimeStr; // 用于前端交互的字符串表示
    @TableField(value = "public_journal")
    private String publicJournal;
    @TableField(value = "is_conference")
    private Boolean isConference;
    @TableField(value = "conference_name")
    private String conferenceName;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "UTC")
    @TableField(value = "conference_time")
    private Date conferenceTime; // 后端存储的Date对象
    @JsonIgnore
    private transient String conferenceTimeStr; // 用于前端交互的字符串表示
    @TableField(value = "static_url")
    private String staticUrl;

    @JsonProperty("tags")
    public List<String> getTagsList() {
        if (tags == null) {
            return Collections.emptyList();
        }

        String[] splitTags = tags.split(";");
        List<String> tagsList = new ArrayList<>();

        for (String tag : splitTags) {
            String trimmedTag = tag.trim();
            if (!trimmedTag.isEmpty()) {
                tagsList.add(trimmedTag);
            }
        }
        return tagsList;
    }

    public void setTagsList(List<String> tagsList) {
        if (tagsList != null) {
            List<String> filteredList = tagsList.stream()
                    .filter(s -> !s.trim().isEmpty())
                    .map(String::trim)
                    .collect(Collectors.toList());
            this.tags = String.join(";", filteredList);
        } else {
            this.tags = null;
        }
    }

    @JsonProperty("keyword")
    public List<String> getKeywordList() {
        if (keyword == null) {
            return Collections.emptyList(); // 如果keyword为null，直接返回空列表
        }

        String[] splitKeywords = keyword.split(";");
        List<String> keywordList = new ArrayList<>();

        for (String s : splitKeywords) {
            if (!s.trim().isEmpty()) { // 过滤掉空字符串
                keywordList.add(s.trim());
            }
        }
        return keywordList;
    }

    public void setKeywordList(List<String> keywordList) {
        if (keywordList != null && !keywordList.isEmpty()) {
            List<String> filteredList = keywordList.stream()
                    .filter(s -> !s.trim().isEmpty())
                    .collect(Collectors.toList());
            this.keyword = String.join(";", filteredList);
        } else {
            this.keyword = null;
        }
    }

    @JsonProperty("author")
    public List<String> getAuthorList() {
        if (author == null) {
            return Collections.emptyList(); // 如果author为null，直接返回空列表
        }

        String[] splitAuthors = author.split(";");
        List<String> authorList = new ArrayList<>();

        for (String auth : splitAuthors) {
            String trimmedAuth = auth.trim();
            if (!trimmedAuth.isEmpty()) {
                authorList.add(trimmedAuth);
            }
        }
        return authorList;
    }

    public void setAuthorList(List<String> authorList) {
        if (authorList != null) {
            List<String> filteredList = authorList.stream()
                    .filter(s -> !s.trim().isEmpty())
                    .map(String::trim)
                    .collect(Collectors.toList());
            this.author = String.join(";", filteredList);
        } else {
            this.author = null;
        }
    }

    @JsonProperty("affiliation")
    public List<String> getAffiliationList() {
        if (affiliation == null) {
            return Collections.emptyList(); // 如果affiliation为null，直接返回空列表
        }

        String[] splitAffiliations = affiliation.split(";");
        List<String> affiliationList = new ArrayList<>();

        for (String aff : splitAffiliations) {
            String trimmedAff = aff.trim();
            if (!trimmedAff.isEmpty()) { // 过滤掉空字符串和仅包含空白字符的字符串
                affiliationList.add(trimmedAff);
            }
        }
        return affiliationList;
    }

    public void setAffiliationList(List<String> newAffiliationList) {
        if (newAffiliationList != null) {
            List<String> filteredList = newAffiliationList.stream()
                    .filter(s -> !s.trim().isEmpty())
                    .map(String::trim)
                    .collect(Collectors.toList());
            this.affiliation = String.join(";", filteredList);
        } else {
            this.affiliation = null;
        }
    }

    public String getDoi() {
        return doi != null ? doi : "";
    }

    public void setDoi(String doi) {
        this.doi = doi != null ? doi : "";
    }

    public String getTitle() {
        return title != null ? title : "";
    }

    public void setTitle(String title) {
        this.title = title != null ? title : "";
    }

    public String getConferenceName() {
        return conferenceName != null ? conferenceName : "";
    }
    public void setConferenceName(String title) {
        this.conferenceName = conferenceName != null ? conferenceName : "";
    }

    public String getStaticUrl() {
        return staticUrl != null ? staticUrl : "";
    }
    public void setStaticUrl(String staticUrl) {
        this.staticUrl = staticUrl != null ? staticUrl : "";
    }

    public String getConferenceTimeStr() {
        if (conferenceTimeStr == null && conferenceTime != null) {
            // 如果字符串表示为空，但Date对象不为空，则进行格式化
            conferenceTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(conferenceTime);
        }
        return conferenceTimeStr;
    }
    public void setConferenceTimeStr(String conferenceTimeStr) throws ParseException {
        this.conferenceTimeStr = conferenceTimeStr;
        // 更新Date对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        this.conferenceTime = sdf.parse(conferenceTimeStr);
    }

    public String getPublicTimeStr() {
        if (publicTimeStr == null && publicTime != null) {
            // 如果字符串表示为空，但Date对象不为空，则进行格式化
            publicTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(publicTime);
        }
        return conferenceTimeStr;
    }
    public void setPublicTimeStr(String publicTimeStr) throws ParseException {
        this.publicTimeStr = publicTimeStr;
        // 更新Date对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        this.publicTime = sdf.parse(publicTimeStr);
    }


}
